package com.example.sffmpegandroidstreamer;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_file;
    private EditText et_url;
    private Button btn_start;

    private final String fileName = "123.mp4";
    private final String rtmpURL = "rtmp://lsspush.zybang.com/zybang/zd_test";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
//        System.loadLibrary("avcodec-56");
//        System.loadLibrary("avdevice-56");
//        System.loadLibrary("avfilter-5");
//        System.loadLibrary("avformat-56");
//        System.loadLibrary("postproc-53");
//        System.loadLibrary("swresample-1");
//        System.loadLibrary("swscale-3");
        System.loadLibrary("ffmpeg");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/" + fileName;
        et_file = findViewById(R.id.et_file);
        et_file.setText(file);
        et_url = findViewById(R.id.et_url);
        et_url.setText(rtmpURL);

        btn_start = findViewById(R.id.btn_start);
        btn_start.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == btn_start.getId()) {
            pushStream(et_file.getText().toString(), et_url.getText().toString(), (int code, String msg) -> {
                //主线程，需要等待调用回调的函数执行结束
                Toast.makeText(this, code + ":" + msg, Toast.LENGTH_SHORT).show();
            });
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native int pushStream(String inputURL, String outputURL, resultCallback callback);
}
